package com.gigi.service.manual;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gigi.entity.JoinCourseEntity;
import com.gigi.enums.ErrorCodeEnum;
import com.gigi.enums.ExamineStateEnum;
import com.gigi.enums.YesOrNoEnum;
import com.gigi.mapper.manual.JoinCourseManualMapper;
import com.gigi.model.*;
import com.gigi.service.generated.JoinCourseService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class JoinCourseManageService {

    private final JoinCourseService joinCourseService;
    private final JoinCourseManualMapper joinCourseManualMapper;

    public JoinCourseManageService(JoinCourseService joinCourseService, JoinCourseManualMapper joinCourseManualMapper) {
        this.joinCourseService = joinCourseService;
        this.joinCourseManualMapper = joinCourseManualMapper;
    }

    /**
     * 新增接口
     *
     * @param request
     * @return
     */
    public Response insert(InsertJoinCourseRequest request) {
        LambdaQueryWrapper<JoinCourseEntity> wrapper = new LambdaQueryWrapper<JoinCourseEntity>()
                .eq(JoinCourseEntity::getCourseId, request.getCourseId())
                .eq(JoinCourseEntity::getUserId, request.getUserId())
                .eq(JoinCourseEntity::getFlag, YesOrNoEnum.YES.getCode())
                .orderByDesc(JoinCourseEntity::getId);
        List<JoinCourseEntity> list = joinCourseService.list(wrapper);

        // 已有待审核 or 审核通过的申请，无需新增
        if (CollUtil.isNotEmpty(list)) {

            if (ExamineStateEnum.UN_CHECK.getCode().equals(list.get(0).getState())) {
                return Response.buildFailure(ErrorCodeEnum.UN_CHECK.getErrCode(), ErrorCodeEnum.UN_CHECK.getErrDesc());
            }

            if (ExamineStateEnum.PASS.getCode().equals(list.get(0).getState())) {
                return Response.buildFailure(ErrorCodeEnum.PASS.getErrCode(), ErrorCodeEnum.PASS.getErrDesc());
            }
        }

        JoinCourseEntity entity = new JoinCourseEntity();
        entity.setUserId(request.getUserId());
        entity.setCourseId(request.getCourseId());
        if (!joinCourseService.save(entity)) {
            return Response.buildFailure(ErrorCodeEnum.SAVE_FAILED.getErrCode(), ErrorCodeEnum.SAVE_FAILED.getErrDesc());
        }
        return Response.buildSuccess();
    }

    /**
     * 学生入课申请列表接口
     *
     * @param request
     * @return
     */
    public MultiResponse<JoinCourseDO> listForStudent(ListExamineRequest request) {
        List<JoinCourseDO> joinCourseDOS = joinCourseManualMapper.listJoinCourseForStudent(request.getUserId());
        return MultiResponse.of(joinCourseDOS, joinCourseDOS.size());
    }

    /**
     * 老师入课申请列表接口
     *
     * @param request
     * @return
     */
    public MultiResponse<JoinCourseDO> listForTeacher(ListExamineRequest request) {
        List<JoinCourseDO> joinCourseDOS = joinCourseManualMapper.listJoinCourseForTeacher(request.getUserId());
        return MultiResponse.of(joinCourseDOS, joinCourseDOS.size());
    }

    /**
     * 审批接口
     *
     * @param request
     * @return
     */
    public Response examine(ExamineRequest request) {
        JoinCourseEntity oldModel = joinCourseService.getById(request.getId());
        if (Objects.isNull(oldModel)) {
            return SingleResponse.buildFailure(ErrorCodeEnum.NO_DATA.getErrCode(), ErrorCodeEnum.NO_DATA.getErrDesc());
        }
        JoinCourseEntity entity = new JoinCourseEntity();
        entity.setId(oldModel.getId());
        entity.setState(request.getState());
        if (!joinCourseService.updateById(entity)) {
            return Response.buildFailure(ErrorCodeEnum.EDIT_FAILED.getErrCode(), ErrorCodeEnum.EDIT_FAILED.getErrDesc());
        }
        return Response.buildSuccess();
    }

    /**
     * 移除接口
     *
     * @param request
     * @return
     */
    public Response move(IdRequest request) {
        JoinCourseEntity oldModel = joinCourseService.getById(request.getId());
        if (Objects.isNull(oldModel)) {
            return Response.buildFailure(ErrorCodeEnum.NO_DATA.getErrCode(), ErrorCodeEnum.NO_DATA.getErrDesc());
        }
        JoinCourseEntity entity = new JoinCourseEntity();
        entity.setId(oldModel.getId());
        entity.setFlag(YesOrNoEnum.NO.getCode());
        if (!joinCourseService.updateById(entity)) {
            return Response.buildFailure(ErrorCodeEnum.EDIT_FAILED.getErrCode(), ErrorCodeEnum.EDIT_FAILED.getErrDesc());
        }
        return Response.buildSuccess();
    }


}
